package ro.gateway.aida.admin;

import java.util.Arrays;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.utils.HttpUtils;

/**
 * Created by IntelliJ IDEA.
 * User: smike
 * Date: Apr 20, 2004
 * Time: 8:22:28 PM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyListSelector$Action extends Action {
	public ActionForward execute(ActionMapping mapping,
						    ActionForm form_,
						    HttpServletRequest request,
						    HttpServletResponse response)
		 throws Exception {

		PersistenceToken token = PersistenceManager.
			 tokenLookup(this.getServlet().getServletContext());
		PropertyListSelector$Form form = (PropertyListSelector$Form) form_;
		form.event = HttpUtils.getValidTrimedString(request, "event", "start");

		if ("start".equals(form.event)) {
			form.action = HttpUtils.getValidTrimedString(request, "action", null);
			form.idxs = null;
			form.selected.clear();

			String s_list = HttpUtils.getValidTrimedString(request, "plist", null);
			form.list = PropertyListDB.getManager(token).getList(s_list);
//			System.out.println(form.list.items.size());
			if (form.list == null) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return null;
			}

			if ("edit".equals(form.action)) {
				String items = HttpUtils.getValidTrimedString(request, "items", null);
				long[] ids = getIds(items);
				for (int i = 0; i < ids.length; i++) {
					form.selected.add(PropertyListDB.getManager(token).getItemById(ids[i]));
				}
			}
		} else if ("delsel".equals(form.event)) {
			int some_idx = HttpUtils.getInt(request, "idxs", -1);
			if (some_idx == -1) form.idxs = null;

			if (form.idxs != null && form.idxs.length > 0) {
				Arrays.sort(form.idxs);
				for (int i = form.idxs.length - 1; i >= 0; i--) {
					form.selected.remove(form.idxs[i]);
				}
			}
		} else if ("add".equals(form.event)) {
			int idx = HttpUtils.getInt(request, "idx", -1);
			if (idx >= 0 && idx < form.list.items.size()) {
				PropertyListItem item = (PropertyListItem) form.list.items.get(idx);
				if (!form.contains(item.getId())) {
					form.selected.add(item);
				}
			}
		}

		return mapping.findForward("jsp_page");
	}

	private long[] getIds(String items) {
		long[] result = new long[0];
		StringTokenizer st = new StringTokenizer(items, ",");
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			long id = -1;
			try {
				id = Long.parseLong(token);
			} catch (Exception ex) {
			}

			if (id != -1) {
				long[] new_res = new long[result.length + 1];
				System.arraycopy(result, 0, new_res, 0, result.length);
				new_res[result.length] = id;
			}
		}

		return result;
	}
}
